import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/Layout.vue'

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表盘' }
      },
      {
        path: 'erp',
        name: 'ERP',
        component: () => import('@/views/erp/Index.vue'),
        meta: { title: 'ERP系统' },
        children: [
          {
            path: 'inventory',
            name: 'ERPInventory',
            component: () => import('@/views/erp/Inventory.vue'),
            meta: { title: '库存管理' }
          },
          {
            path: 'purchase',
            name: 'ERPPurchase',
            component: () => import('@/views/erp/Purchase.vue'),
            meta: { title: '采购管理' }
          },
          {
            path: 'sales',
            name: 'ERPSales',
            component: () => import('@/views/erp/Sales.vue'),
            meta: { title: '销售管理' }
          }
        ]
      },
      {
        path: 'mes',
        name: 'MES',
        component: () => import('@/views/mes/Index.vue'),
        meta: { title: 'MES系统' },
        children: [
          {
            path: 'workorder',
            name: 'MESWorkOrder',
            component: () => import('@/views/mes/WorkOrder.vue'),
            meta: { title: '工单管理' }
          },
          {
            path: 'process',
            name: 'MESProcess',
            component: () => import('@/views/mes/Process.vue'),
            meta: { title: '工艺管理' }
          },
          {
            path: 'equipment',
            name: 'MESEquipment',
            component: () => import('@/views/mes/Equipment.vue'),
            meta: { title: '设备管理' }
          },
          {
            path: 'quality',
            name: 'MESQuality',
            component: () => import('@/views/mes/Quality.vue'),
            meta: { title: '质量追溯' }
          }
        ]
      },
      {
        path: 'production',
        name: 'Production',
        component: () => import('@/views/production/Index.vue'),
        meta: { title: '生产管理' },
        children: [
          {
            path: 'schedule',
            name: 'ProductionSchedule',
            component: () => import('@/views/production/Schedule.vue'),
            meta: { title: '生产排产' }
          },
          {
            path: 'kanban',
            name: 'ProductionKanban',
            component: () => import('@/views/production/Kanban.vue'),
            meta: { title: '现场看板' }
          },
          {
            path: 'material',
            name: 'ProductionMaterial',
            component: () => import('@/views/production/Material.vue'),
            meta: { title: '物料拉动' }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
